var userCode=null;
var password=null;
var repassword=null;
var userName=null;
var phone=null;
var email=null;
var add=null;
var back=null;
var userSchool=null;
$(function() {
    userCode = $("#userCode");
    password=$("#userPassword");
    repassword=$("#rePassword");
    userName=$("#userName");
    phone=$("#userPhone");
    email=$("#email");
    add=$("#add")
    back=$("#back");
    userSchool=$("#userSchool")

    userCode.next().html("*");
    password.next().html("*");
    repassword.next().html("*");
    userName.next().html("*");
    phone.next().html("*");
    email.next().html("*");


    //用户名异步验证
    userCode.bind("blur",function(){
        //ajax后台验证--userCode是否已存在
        var patten=/^[a-zA-Z]\w{5,9}$/;
        if(userCode.val() == null || userCode.val() == ""){
            validateTip(userCode.next(),{"color":"red"}, "用户名不能为空",false);
        }else if(!patten.test(userCode.val())){
            validateTip(userCode.next(),{"color":"red"},"以字母开头，长度在6~10之间",false);
        } else {
            //接应
            $.ajax({
                type: "GET",
                dataType: 'json',
                data: {userCode: userCode.val()},
                url: "user/ucexist.html",
                success: function (data) {
                    if (data.userCode == "noexist") {
                        validateTip(userCode.next(), {"color": "green"}, "该账号可注册", true);
                    } else if (data.userCode == "exist") {
                        validateTip(userCode.next(), {"color": "red"}, " 该用户账号已存在", false);
                    }
                }
            })
        }
    }).bind("focus",function(){
        //显示友情提示
        validateTip(userCode.next(),{"color":"#666666"},"* 账号是您登录系统的账号",false);
    }).focus();
    //密码验证
    password.bind("focus",function(){
        validateTip(password.next(),{"color":"#666666"},"* 长度必须是大于6小于10",false);
    }).bind("blur",function(){
        if(password.val() != null && password.val().length > 6
            && password.val().length < 10 ){
            validateTip(password.next(),{"color":"green"},"密码可用哦!",true);
        }else{
            validateTip(password.next(),{"color":"red"}, " 密码输入不符合规范",false);
        }
    });
    //确认密码验证
    repassword.bind("focus",function(){
        validateTip(repassword.next(),{"color":"#666666"},"* 请输入与上面一只的密码",false);
    }).bind("blur",function(){
        if(repassword.val() != null && repassword.val().length > 6
            && repassword.val().length < 10 && password.val() == repassword.val()){
            validateTip(repassword.next(),{"color":"green"},"两次密码相同",true);
        }else{
            validateTip(repassword.next(),{"color":"red"}, " 两次密码输入不一致",false);
        }
    });
    //用户名验证
    userName.bind("focus",function(){
        validateTip(userName.next(),{"color":"#666666"},"* 长度必须是大于1小于10的字符",false);
    }).bind("blur",function(){
        if(userName.val() != null && userName.val().length > 1
            && userName.val().length < 10){
            validateTip(userName.next(),{"color":"green"}," ",true);
        }else{
            validateTip(userName.next(),{"color":"red"}," 用户名输入的不符合规范",false);
        }

    });
    //手机验证
    phone.bind("focus",function(){
        validateTip(phone.next(),{"color":"#666666"},"* 请输入手机号",false);
    }).bind("blur",function(){
        var patrn=/^(13[0-9]|15[0-9]|18[0-9])\d{8}$/;
        if(phone.val().match(patrn)){
            validateTip(phone.next(),{"color":"green"}," ",true);
        }else{
            validateTip(phone.next(),{"color":"red"}, " 您输入的手机号格式不正确",false);
        }
    });

    //邮箱验证
    email.bind("focus",function () {
        validateTip(email.next(),{"color":"#666666"},"* 请输入邮箱",false);
    }).bind("blur",function () {
        var reg = new RegExp("^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$");
        if(email.val()==""||email.val()==null){
            validateTip(email.next(),{"color":"red"}, "邮箱不能为空",false);
        }else if(!reg.test(email.val())){
            validateTip(email.next(),{"color":"red"}, "请仔细检查邮箱格式",false);
        }else{
            validateTip(email.next(),{"color":"green"}, "该邮箱可用",true);
        }
    })


    //数据提交
    add.bind("click",function(){
        if(userCode.attr("validateStatus") !== "true"){
            userCode.blur();
        }else if(userName.attr("validateStatus") !== "true"){
            userName.blur();
        }else if(password.attr("validateStatus") !== "true"){
            password.blur();
        }else if(repassword.attr("validateStatus") !== "true"){
            repassword.blur();
        }else if(email.attr("validateStatus") !== "true"){
            email.blur();
        }else if(phone.attr("validateStatus") !== "true"){
            phone.blur();
        }else if(userSchool.val==0){
            validateTip(userSchool.next(),{"color":"red"}, "请选择学校",false);
        }else{
            if(confirm("是否确认提交数据")){
                $("#addUser").submit();
                alert("注册成功")
            }
        }
    });
});
